<template>
  <div class="app-container">
    <el-form class="formWidth" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="接访类别" prop="receiveCategory">
        <el-select v-model="queryParams.receiveCategory" placeholder="请选择接访类别" clearable size="small">
          <el-option
            v-for="dict in receiveCategoryOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="接访人" prop="receiver">
        <el-input
          v-model="queryParams.receiver"
          placeholder="请输入接访人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="接访人地址" prop="address">-->
<!--        <el-input-->
<!--          v-model="queryParams.address"-->
<!--          placeholder="请输入接访人地址"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="投诉对象" prop="complainant">
        <el-input
          v-model="queryParams.complainant"
          placeholder="请输入投诉对象"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phoneNo">
        <el-input
          v-model="queryParams.phoneNo"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['comprehensive:visits:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['comprehensive:visits:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['comprehensive:visits:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['comprehensive:visits:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="visitsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="接访类别" :show-overflow-tooltip="true" align="center" prop="receiveCategory" :formatter="receiveCategoryFormat" />
      <el-table-column label="接访人" :show-overflow-tooltip="true" align="center" prop="receiver" />
<!--      <el-table-column label="接访人地址" :show-overflow-tooltip="true" align="center" prop="address" />-->
      <el-table-column label="投诉对象" :show-overflow-tooltip="true" align="center" prop="complainant" />
      <el-table-column label="标题" :show-overflow-tooltip="true" align="center" prop="title" />
      <el-table-column label="内容" :show-overflow-tooltip="true" align="center" prop="content" />
      <el-table-column label="联系电话" :show-overflow-tooltip="true" align="center" prop="phoneNo" />
      <el-table-column label="创建时间" :show-overflow-tooltip="true" align="center" prop="createTime" width="180">
        <template slot-scope="scope" :show-overflow-tooltip="true">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['comprehensive:visits:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['comprehensive:visits:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改接访信息对话框 -->
    <el-dialog class="formWidth" :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-row :gutter="20" >
          <el-col :span="12" >
            <el-form-item label="接访类别" prop="receiveCategory">
              <el-select v-model="form.receiveCategory" placeholder="请选择接访类别">
                <el-option
                  v-for="dict in receiveCategoryOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="接访人" prop="receiver">
              <el-input v-model="form.receiver" placeholder="请输入接访人" />
            </el-form-item>
          </el-col>

          <el-col :span="12" >
            <el-form-item label="接访人地址" prop="addressId">
              <el-cascader
                v-model="form.addressId"
                :options="options"
                :props="propList"
                @change="handleChange"></el-cascader>
            </el-form-item>
          </el-col>

          <el-col :span="12" >
            <el-form-item label="详细地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入详细地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="投诉对象" prop="complainant">
              <el-input v-model="form.complainant" placeholder="请输入投诉对象" />
            </el-form-item>
          </el-col>

          <el-col :span="12" >
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="联系电话" prop="phoneNo">
            <el-input v-model="form.phoneNo" placeholder="请输入联系电话" />
          </el-form-item>
          </el-col>
          <el-col :span="24" >
            <el-form-item label="内容">
              <el-input
                type="textarea"
                placeholder="请输入内容"
                v-model="form.content"
                rows="5"
                maxlength="500"
                show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVisits, getVisits, delVisits, addVisits, updateVisits, exportVisits } from "@/api/comprehensive/visits";
import { cityTree } from '@/api/care/care_retirefm'

export default {
  name: "Visits",
  data() {
    return {
      propList:{
        expandTrigger:"hover",
        value:"id",
        label:"cityName",
        children:"cityChildren"
      },
      options:[],

      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 接访信息表格数据
      visitsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 接访类别字典
      receiveCategoryOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        receiveCategory: null,
        receiver: null,
        address: null,
        complainant: null,
        title: null,
        content: null,
        phoneNo: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        receiveCategory: [
          { required: true, message: "接访类别不能为空", trigger: "change" }
        ],
        receiver: [
          { required: true, message: "接访人不能为空", trigger: "blur" }
        ],
        addressId: [
          { required: true, message: "接访人地址不能为空", trigger: "blur" }
        ],
        complainant: [
          { required: true, message: "投诉对象不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
        phoneNo: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("visit_type").then(response => {
      this.receiveCategoryOptions = response.data;
    });
    this.cityTree()
  },
  methods: {
    // 省市区三级联动
    cityTree(){
      cityTree().then(response =>{
        console.log("city",response)
        this.options = response.data;
      });
    },
    handleChange(value) {
      console.log(value);
    },
    /** 查询接访信息列表 */
    getList() {
      this.loading = true;
      listVisits(this.queryParams).then(response => {
        this.visitsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 接访类别字典翻译
    receiveCategoryFormat(row, column) {
      return this.selectDictLabel(this.receiveCategoryOptions, row.receiveCategory);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        receiveCategory: null,
        receiver: null,
        address: null,
        complainant: null,
        title: null,
        content: null,
        attachmentUrl: null,
        phoneNo: null,
        createTime: null,
        recorder: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加接访信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVisits(id).then(response => {
        this.form = response.data;

        let addressId = response.data.addressId;
        let birthIdStr = addressId ? addressId.split(',') : null;
        let birthIdArr = birthIdStr ? birthIdStr.map(item => {
          return Number(item)
        }): null;
        this.$set(this.form , 'addressId',birthIdArr)

        this.open = true;
        this.title = "修改接访信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.addressId = this.form.addressId ? this.form.addressId.join(",") : null;
          if (this.form.id != null) {
            updateVisits(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVisits(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该项数据?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delVisits(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有接访信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportVisits(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
